﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace SecureCommander.Core.SecurityManager
{
    public class TripleDesAlgorithm
    {
        private TripleDESCryptoServiceProvider TripleDes;
        public byte[] Key;
        public byte[] IV;
        public TripleDesAlgorithm(PaddingMode pad,CipherMode cipher)
        {
            TripleDes = new TripleDESCryptoServiceProvider();
            TripleDes.GenerateIV();
            IV = TripleDes.IV;
            TripleDes.GenerateKey();
            Key = TripleDes.Key;
            TripleDes.Padding = pad;
            TripleDes.Mode = cipher;
        }
        public TripleDesAlgorithm(PaddingMode pad, CipherMode cipher, byte[] Key, byte[] IV)
        {
            TripleDes = new TripleDESCryptoServiceProvider();
            TripleDes.IV = IV;
            TripleDes.Key = Key;
            TripleDes.Padding = pad;
            TripleDes.Mode = cipher;
        }

        public TripleDesAlgorithm()
        {
            TripleDes = new TripleDESCryptoServiceProvider();
            TripleDes.GenerateIV();
            TripleDes.GenerateKey();
        }

        public bool EncryptFile(String nameFileInput, String nameFileOutput)
        {
            FileStream in_fStream = File.Open(nameFileInput, FileMode.Open, FileAccess.Read);
            FileStream out_fStream = File.Open(nameFileOutput, FileMode.OpenOrCreate, FileAccess.Write);

            try
            {
                CryptoStream cStream = new CryptoStream(out_fStream, TripleDes.CreateEncryptor(), CryptoStreamMode.Write);
                BinaryWriter sWriter = new BinaryWriter(cStream);
                in_fStream.Close();
                byte[] input;
                input = File.ReadAllBytes(nameFileInput);
                try
                {
                    sWriter.Write(input);
                }
                catch (Exception)
                {
                    return false;
                }

                sWriter.Close();
                cStream.Close();
            }
            catch (CryptographicException)
            {
                return false;
            }
            finally
            {
                out_fStream.Close();
            }
            return true;
        }


        public bool DecryptFile(String nameFileInput, String nameFileOutput)
        {
            FileStream in_fStream = File.Open(nameFileInput, FileMode.Open, FileAccess.Read);
            FileStream out_fStream = File.Open(nameFileOutput, FileMode.OpenOrCreate, FileAccess.Write);

            try
            {CryptoStream cStream = new CryptoStream(out_fStream, TripleDes.CreateDecryptor(), CryptoStreamMode.Write);

                BinaryWriter sWriter = new BinaryWriter(cStream);
                in_fStream.Close();
                byte[] input;
                input = File.ReadAllBytes(nameFileInput);
                try
                {
                    sWriter.Write(input);
                }
                catch (Exception)
                {
                    return false;
                }
                sWriter.Close();
                cStream.Close();
            }
            catch (CryptographicException)
            {
                return false;
            }
            finally
            {
                out_fStream.Close();
            }
            return true;
        }

    }
}
